<?php
/**
 * CashFlow.php
 * Author: luoji (867404364@qq.com)
 * Date: 2020-12-22
 * Time: 11:09
 */

namespace App\Http\Controllers\Api\Finance;



use App\Http\Controllers\Base;
use App\Tables;

class CashFlow extends Base
{
    public function get()
    {
        $firstDate = $this->request->get('firstDate');
        $lastDate  = $this->request->get('lastDate');

        $table = Tables\CashFlow::query();
        if(!empty($firstDate) and !empty($lastDate)) {
            $table->whereBetween('add_time', [strtotime($firstDate), strtotime($lastDate) + 86400]);
        } else {
            $firstDate = date('Ym01');
            $lastDate  = date('Ym01',strtotime('+1 month'));
            $table->whereBetween('add_time', [strtotime($firstDate), strtotime($lastDate)]);
        }

        $list = $table->orderBy('id', 'desc')->get();
        $r = [];
        foreach($list as $v) {
            $r[] = [
                'id' => $v['id'],
                'addTime' => date('Y-m-d H:i:s', $v['add_time']),
                'income' => $v['change'] >  0 ? $v['change'] :0,
                'outcome' => $v['change'] < 0 ? abs($v['change']) :0,
                'desc' => $v['desc']
            ];
        }

        return $this->responseSuccess($r);
    }

}